package web.controller.user;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.FavoriteService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.FavoriteRequest;
import web.security.Protection;

/**
 * Created by zf on 2015/7/17.
 */


@RestController
@RequestMapping(Routes.User.FAVORITE)
public class Favorite extends Base {

    @Autowired
    FavoriteService favoriteService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse favorite(@RequestBody FavoriteRequest request) {
        long userId = getUserIdFromAuthHeader();
        request.setUserId(userId);
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return fail(result.getMessage());
        }

        return favoriteService.favorite(userId, request.getFavorUserId());
    }
}
